import Vue from 'vue'
import VueRouter from 'vue-router'

// import moduleName from '../components/Login.vue';

Vue.use(VueRouter)

const routes = [{
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'login',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () =>
            import ( /* webpackChunkName: "login_home_welcome" */ '../components/Login.vue')
    },
    {
        path: '/home',
        name: 'home',
        redirect: '/welcome',
        component: () =>
            import ( /* webpackChunkName: "login_home_welcome" */ '../components/Home.vue'),
        children: [{
                path: '/welcome',
                name: 'welcome',
                component: () =>
                    import ( /* webpackChunkName: "login_home_welcome" */ '../components/Welcome.vue')
            },

            // ============================================================================================================
            {
                path: '/users',
                name: 'users',
                component: () =>
                    import ( /* webpackChunkName: "users_rights_roles" */ '../components/users/Users.vue')
            },
            {
                path: '/rights',
                name: 'rights',
                component: () =>
                    import ( /* webpackChunkName: "users_rights_roles" */ '../components/rights/RightsList.vue')
            },
            {
                path: '/roles',
                name: 'roles',
                component: () =>
                    import ( /* webpackChunkName: "users_rights_roles" */ '../components/rights/Roles.vue')
            },
            // ============================================================================================================
            {
                path: '/categories',
                name: 'categories',
                component: () =>
                    import ( /* webpackChunkName: "goods_power" */ '../components/goods/Categories.vue')
            },
            {
                path: '/params',
                name: 'params',
                component: () =>
                    import ( /* webpackChunkName: "goods_power" */ '../components/goods/Params.vue')
            },
            {
                path: '/goods',
                name: 'goods',
                component: () =>
                    import ( /* webpackChunkName: "goods_power" */ '../components/goods/GoodsList.vue')
            },
            {
                path: '/goods/add_goods',
                name: 'goods/add_goods',
                component: () =>
                    import ( /* webpackChunkName: "goods_power" */ '../components/goods/AddGoods.vue')
            },
            // ============================================================================================================
            {
                path: '/orders',
                name: 'orders',
                component: () =>
                    import ( /* webpackChunkName: "orders_reports" */ '../components/orders/OrderList.vue')
            },
            {
                path: '/reports',
                name: 'reports',
                component: () =>
                    import ( /* webpackChunkName: "orders_reports" */ '../components/data_statistics/Reports.vue')
            }
        ]
    }
]

const router = new VueRouter({
    routes
})

router.beforeEach((to, form, next) => {
    if (to.path == '/login') return next()
    const token = window.sessionStorage.getItem('token')
    if (!token) return next('/login')
        // window.sessionStorage.setItem('active_path', to.path)
    next()

})

export default router